﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;

using SSATool.DL.Server.WindowsService.Properties;
using SSATool.Common.Util.ExtensionMethods;
using SSATool.Common.Util.Threading;

#endregion

namespace SSATool.DL.Server.WindowsService
{
    /// <summary>
    /// Contains the windows service entry point, which starts the server
    /// components of Simpile Service Administration Tool.
    /// </summary>
    partial class SSAToolService : ServiceBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private AsyncProcess _adminSvcProc;
        private AsyncProcess _eventSvcProc;

        private ServiceAdminWcfServer _adminSvcHost;
        private ServiceEventWcfServer _eventSvcHost;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="SSAToolService"/> class.
        /// </summary>
        public SSAToolService()
        {
            InitializeComponent();

            _adminSvcHost = new ServiceAdminWcfServer();
            _eventSvcHost = new ServiceEventWcfServer();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry(Resources.LogEventOnStart, EventLogEntryType.Information);

            EventLog.WriteEntry(Resources.LogEventStartAdminService, EventLogEntryType.Information);
            _adminSvcProc = new AsyncProcess();
            _adminSvcProc.ThreadException += OnServiceException;
            
            EventLog.WriteEntry(Resources.LogEventStartEventService, EventLogEntryType.Information);
            _eventSvcProc = new AsyncProcess();
            _eventSvcProc.ThreadException += OnServiceException;

            _adminSvcProc.Run(_adminSvcHost.Start);
            _eventSvcProc.Run(_eventSvcHost.Start);

            EventLog.WriteEntry(Resources.LogEventOnStartCompleted, EventLogEntryType.Information);
        }

        protected override void OnStop()
        {
            EventLog.WriteEntry(Resources.LogEventOnStop, EventLogEntryType.Information);

            _adminSvcProc.ThreadException -= OnServiceException;
            _eventSvcProc.ThreadException -= OnServiceException;

            try
            {
                _adminSvcHost.Stop();
                _eventSvcHost.Stop();
            }
            catch (Exception e)
            {
                _adminSvcHost.Dispose();
                _eventSvcHost.Dispose();

                PrintException(e);
            }
        }

        private void PrintException(Exception e)
        {
            EventLog.WriteEntry(
                string.Format(Resources.LogEventOnError, e.ToFullErrorString()),
                EventLogEntryType.Error);
        }

        private void HandleException(Exception e)
        {
            if (e == null)
                return;

            PrintException(e);
            Stop();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private void OnServiceException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        #endregion
    }
}
